//
//  ManagerViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/11/8.
//  Copyright © 2016年 KKING. All rights reserved.
//

#import "ManagerCollectionViewCellSecond.h"
#import "ManagerViewController.h"
#import "ManagerCollectionViewCell.h"
#import "Masonry.h"
//#import "ZXSetTableViewController.h"
#import "SNSetViewController.h"

@interface ManagerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/** View */
@property (weak, nonatomic) UICollectionView * collectionView;
/**  UIPageControl * _pageControl; */
@property (nonatomic,strong)  UIPageControl * pageControl;
/** View */
@property (strong, nonatomic) UIView * bgView;
/** imageView */
@property (weak, nonatomic) UIImageView * backImageView;
@end

@implementation ManagerViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:1.0];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIImageView * backImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        UIImage * image = [UIImage imageNamed:@"Rectangle_1"];
        imageView.image = image;
        
        [self.view insertSubview:imageView atIndex:0];
        
        [imageView mas_makeConstraints:^(MASConstraintMaker * make) {
            make.left.mas_equalTo(self.view.mas_left).offset(0);
            make.right.mas_equalTo(self.view.mas_right).offset(0);
            make.top.mas_equalTo(self.view.mas_top).offset(0);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        }];
        
        imageView;
    });
    self.backImageView = backImageView;
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64) collectionViewLayout:flowLayout];
    self.collectionView = collectionView;
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.pagingEnabled = YES;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerNib:[UINib nibWithNibName:@"ManagerCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"ManagerCollectionViewCell"];
    [collectionView registerNib:[UINib nibWithNibName:@"ManagerCollectionViewCellSecond" bundle:nil] forCellWithReuseIdentifier:@"ManagerCollectionViewCellSecond"];
    [self.view addSubview:collectionView];
    
    //sn_note:=========  ============================ stone 🐳 ===========/
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 20, [UIScreen mainScreen].bounds.size.width, 16)];
    _pageControl.currentPage = 0;
    _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    _pageControl.userInteractionEnabled = NO;
    _pageControl.numberOfPages = 2;
    [self.view addSubview:_pageControl];
    //sn_note:=========  ============================ stone 🐳 ===========/
    UIImage * image = [UIImage imageNamed:@"icon_profile"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(infoBtnClick:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    //sn_note:=========  ============================ stone 🐳 ===========/
//    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
//    self.navigationController.navigationBar.translucent = YES;
//    [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//    self.view.backgroundColor = [UIColor clearColor];
//    self.bgView.backgroundColor = [UIColor clearColor];
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
//        
//        
//        [self.navigationController.navigationBar insertSubview:self.bgView atIndex:0];
//    });
    
}
- (UIView *)bgView {
    
    if (_bgView == nil) {
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 64)];
        
    }
    return _bgView;
}
-(void)infoBtnClick:(UIBarButtonItem *)sender {
    UIViewController *setSb = [[SNSetViewController alloc]init];
//    [[UIStoryboard storyboardWithName:@"ZXSetTableViewController" bundle:nil] instantiateInitialViewController];
    [self.navigationController pushViewController:setSb animated:YES];
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 2;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell * cell = nil;
    
    switch (indexPath.item) {
        case 0:
        {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ManagerCollectionViewCell" forIndexPath:indexPath];
            [cell setValue:self.nav forKey:@"nav"];
        }
            break;
        case 1:
        {
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ManagerCollectionViewCellSecond" forIndexPath:indexPath];
            [cell setValue:self.nav forKey:@"nav"];
        }
            break;
        default:
            break;
    }
    cell.contentView.backgroundColor = [UIColor clearColor];
    return cell;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.x / [UIScreen mainScreen].bounds.size.width > 0.5) {
        _pageControl.currentPage = 1;
    }else{
        _pageControl.currentPage = 0;
    }
}
//sn_note:=========  ============================ stone 🐳 ===========/
/** 16进制(字符串形式) 返回 图片 */
- (UIImage *)imageWithString:(NSString *)string {
    
    return [self imageWithColor:[self colorWithString:string]];
}
/** 16进制返回颜色 */
- (UIColor *)colorWithString:(NSString *)string {
    CGFloat alpha = 1.0f;
    NSNumber * red = @0.0;
    NSNumber * green = @0.0;
    NSNumber * blue = @0.0;
    
    NSMutableArray<NSNumber *> * colors = [NSMutableArray arrayWithArray:@[ red, green, blue ]];
    
    unsigned hexComponent;
    NSString * colorString = [string uppercaseString];
    NSLog(@"%ld", colors.count);
    for (int i = 0; i < colors.count; i++) {
        NSString * substring = [colorString substringWithRange:NSMakeRange(i * 2, 2)];
        
        [[NSScanner scannerWithString:substring] scanHexInt:&hexComponent];
        
        NSNumber * temp = colors[i];
        temp = @(hexComponent / 255.0);
        colors[i] = temp;
    }
    
    return [UIColor colorWithRed:[colors[0] floatValue] green:[colors[1] floatValue] blue:[colors[2] floatValue] alpha:alpha];
}
/** 颜色 返回 图片 */
- (UIImage *)imageWithColor:(UIColor *)color {
    
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
#pragma mark - <UINavigationControllerDelegate>

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
}

@end
